package com.fatcat.easybuy.controller;

import com.fatcat.easybuy.annotaion.CheckPermission;
import com.fatcat.easybuy.pojo.EasyBuyShoppingCart;
import com.fatcat.easybuy.service.EasyBuyProductService;
import com.fatcat.easybuy.service.EasyBuyShoppingCartService;
import com.fatcat.easybuy.util.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 李开辰
 * @since 2025/9/19 10:42
 */
@RestController
@RequestMapping("/shoppingCart")
public class EasyBuyShoppingCartController {
    @Autowired
    private EasyBuyShoppingCartService easyBuyShoppingCartService;
    @Autowired
    EasyBuyProductService easyBuyProductService;

    private static final Logger LOGGER = LoggerFactory.getLogger(EasyBuyShoppingCartController.class);

    //添加购物车
    @CheckPermission({"0", "1"})
    @PostMapping("/addProductToCart")
    public ResponseResult addCarProduct(
            @Validated @RequestBody EasyBuyShoppingCart easyBuyShoppingCart,
            @RequestAttribute("userId") Long userId) {
        LOGGER.info("收到购物车请求 - 用户ID: {}, 商品: {}", userId, easyBuyShoppingCart);
        if (easyBuyProductService.getProductById(easyBuyShoppingCart.getProductId()) == null) {
            return ResponseResult.error("401", "商品不存在");
        }
        if (!easyBuyShoppingCartService.addProductToCart(userId, easyBuyShoppingCart)) {
            return ResponseResult.error("500", "购物车添加错误");
        }
        return ResponseResult.success("购物车添加成功");
    }

    @DeleteMapping("/deleteProductFromCart")
    public ResponseResult deleteProductFromCart(
            @RequestAttribute("userId") Long userId,
            @RequestParam("productId") Long productId) {
        if (userId == null || productId == null) {
            return ResponseResult.error("401", "缺少参数:userId或productId");
        }
        if (easyBuyShoppingCartService.deleteProductFromCart(userId, productId)) {
            return ResponseResult.success("购物车删除成功");
        }
        return ResponseResult.error("500", "购物车删除错误");
    }

    //更新购物车
    @CheckPermission({"0", "1"})
    @PostMapping("/updateProductInCart")
    public ResponseResult updateProductInCart(
            @Validated @RequestBody EasyBuyShoppingCart cart,
            @RequestAttribute("userId") Long userId) {
        LOGGER.info("更新购物车 - 用户ID: {}, 商品: {}", userId, cart);

        if (cart.getProductId() == null) {
            return ResponseResult.error("401", "缺少参数:productId");
        }

        if (cart.getQuantity() == null || cart.getQuantity() < 1) {
            return ResponseResult.error("401", "商品数量必须大于0");
        }

        if (cart.getPrice() != null) {
            cart.setCost(cart.getPrice() * cart.getQuantity());
        }

        if (easyBuyShoppingCartService.updateProductInCart(userId, cart)) {
            return ResponseResult.success("购物车更新成功");
        }
        return ResponseResult.error("500", "购物车更新错误");
    }

    //查询购物车
    @CheckPermission({"0", "1"})
    @GetMapping("/getShoppingCart")
    public ResponseResult getShoppingCart(@RequestAttribute("userId") Long userId) {

        List<EasyBuyShoppingCart> list = easyBuyShoppingCartService.getShoppingCartList(userId);
        return ResponseResult.success().add("list", list);
    }

    //清空购物车
    @CheckPermission({"0", "1"})
    @DeleteMapping("/clearShoppingCart")
    public ResponseResult clearShoppingCart(@RequestAttribute("userId") Long userId) {
        if (easyBuyShoppingCartService.clearShoppingCart(userId)) {
            return ResponseResult.success("购物车清空成功");
        }
        return ResponseResult.error("500", "购物车清空错误");
    }
}


